package cn.kgc.novel.controller;

import cn.kgc.novel.common.ErrorCodeEnum;
import cn.kgc.novel.common.Result;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController(value = "taskController")
@CrossOrigin
public class TaskController {
    @Autowired
    @Qualifier(value = "Scheduler")
    private Scheduler scheduler;

    /**
     * 新建任务
     * jobName   任务名称
     * jobGroup 任务分组
     * **/
    @GetMapping("/createjob")
    public Result testschedule(String jobName, String jobGroup,Integer bid,Integer uid,Integer titleid, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date executeDate){
        System.out.println("testschedule中的executeDate"+executeDate);
        try {
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //构建job信息
            JobDetail job = JobBuilder.newJob(CruisePlanJob.class).withIdentity(jobName, jobGroup)
                    .withDescription("巡航计划").build();
            JobDataMap jobDataMap = job.getJobDataMap();
            jobDataMap.put("cruiseId", "1");
            jobDataMap.put("bid",bid);
            jobDataMap.put("uid",uid);
            jobDataMap.put("titleid",titleid);
            //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("cron的表达式");
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger" + jobName, jobGroup)
                    .startAt(executeDate)
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0))
                    .build();
            //SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0).withIntervalInSeconds(20)//每隔多少秒执行一次； withRepeatCount 设置重复的次数
            //.startNow().withSchedule(cronScheduleBuilder)
            //交由Scheduler安排触发

            scheduler.scheduleJob(job, trigger);
            System.out.println("startJob:"+JobKey.jobKey(jobName));
        } catch (SchedulerException e) {
            e.printStackTrace();
            return Result.error(ErrorCodeEnum.OPERATION_FAILED);
        }
        return Result.ok();
    }


    /**
     * 删除任务
     * **/

    @GetMapping("removejob")
    public Result remove(String jobName, String jobGroup) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除任务
            boolean result = scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
            System.out.println(result);
            System.out.println("removeJob:"+JobKey.jobKey(jobName));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ErrorCodeEnum.OPERATION_FAILED);
        }
        return Result.ok();
    }
    //恢复任务
    /*scheduler.resumeJob(key);
    //停止任务
    scheduler.pauseJob(key);*/
}
